//package com.chen.springboot.controller;
//
//import com.chen.springboot.entity.Menu;
//import com.chen.springboot.service.Impl.MenuService;
////import com.example.menu.entity.Menu;
////import com.example.menu.service.MenuService;
//import io.swagger.annotations.Api;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//@Api(tags = {"路由接口"})
//@RequestMapping("/items")
//@RestController
//public class MenuController {
//    private final MenuService menuService;
//
//    public MenuController(MenuService menuService) {
//        this.menuService = menuService;
//    }
//
//    @GetMapping("/menus")
//    public Map<String, Object> getMenus() {
//        List<Menu> menuTree = menuService.getMenuTree();
//        Map<String, Object> resp = new HashMap<>();
//        resp.put("code", 200);
//        resp.put("data", menuTree);
//        resp.put("msg", "成功");
//        return resp;
//    }
//
//}

package com.chen.springboot.controller.web;

import com.chen.springboot.entity.Menu;
import com.chen.springboot.service.Impl.MenuService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = {"路由接口"})
@RequestMapping("/items")
@RestController
public class MenuController {

    private final MenuService menuService;

    public MenuController(MenuService menuService) {
        this.menuService = menuService;
    }
    @GetMapping("/menus")
    public Map<String, Object> getMenus() {
        // 获取经过权限过滤后的菜单
        List<Menu> menuTree = menuService.getMenuTree();
        Map<String, Object> resp = new HashMap<>();
        resp.put("code", 200);
        resp.put("data", menuTree);
        resp.put("msg", "成功");
        return resp;
    }

    // 新增菜单接口
    @PostMapping("/add/menus")
    public Map<String, Object> addMenu(@RequestBody Menu menu) {
        menuService.addMenu(menu);
        Map<String, Object> resp = new HashMap<>();
        resp.put("code", 200);
        resp.put("msg", "菜单添加成功");
        return resp;
    }

    // ✅ 修改菜单接口
//    @PutMapping("/menus/{id}")
//    public Map<String, Object> updateMenu(@PathVariable Integer id, @RequestBody Menu menu) {
//        menu.setId(id);  // 保证 ID 设置
//        int result = menuService.updateMenu(menu);
//        Map<String, Object> resp = new HashMap<>();
//        if (result > 0) {
//            resp.put("code", 200);
//            resp.put("msg", "菜单更新成功");
//        } else {
//            resp.put("code", 500);
//            resp.put("msg", "菜单更新失败");
//        }
//        return resp;
//    }

    //修改菜单接口
    @PutMapping("/menus")
    public Map<String, Object> updateMenu(@RequestBody Menu menu) {
        int result = menuService.updateMenu(menu);
        Map<String, Object> resp = new HashMap<>();
        if (result > 0) {
            resp.put("code", 200);
            resp.put("msg", "菜单更新成功");
        } else {
            resp.put("code", 500);
            resp.put("msg", "菜单更新失败");
        }
        return resp;
    }

    // 删除菜单接口
    // 删除菜单接口
    @DeleteMapping("/menus/{id}")
    public Map<String, Object> deleteMenu(@PathVariable Integer id) {
        int result = menuService.deleteMenu(id);
        Map<String, Object> resp = new HashMap<>();
        if (result > 0) {
            resp.put("code", 200);
            resp.put("msg", "菜单删除成功");
        } else {
            resp.put("code", 500);
            resp.put("msg", "菜单删除失败");
        }
        return resp;
    }

}



